package com.wwh.furn.service.impl;

import com.wwh.furn.bean.Furn;
import com.wwh.furn.bean.FurnExample;
import com.wwh.furn.dao.FurnMapper;
import com.wwh.furn.service.FurnService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * @author Albert
 * @date 2024/12/19-20:05
 * @describe
 */
@Service
public class FurnServiceImpl implements FurnService {
    //注入FurnService代理对象
    @Resource
    private FurnMapper furnMapper;

    @Override
    public void save(Furn furn) {
        //由于id是自增长的，不需要指定id，selective方法会判断如果id不为空才进行拼接
        furnMapper.insertSelective(furn);
    }

    @Override
    public List<Furn> findAll() {
        //查看FurnMapper.xml文件
        //传入的是null，表示返回所有的家居
        return furnMapper.selectByExample(null);
    }

    @Override
    public void update(Furn furn) {

        furnMapper.updateByPrimaryKeySelective(furn);
    }

    @Override
    public void delete(Integer id) {
        furnMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Furn findFurnById(Integer id) {
        furnMapper.selectByPrimaryKey(id);
        return furnMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Furn> findByCondition(String name) {
        FurnExample furnExample = new FurnExample();
        //通过Criteria对象设置查询条件
        FurnExample.Criteria criteria = furnExample.createCriteria();
        if (StringUtils.hasText(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        return furnMapper.selectByExample(furnExample);
    }
}
